package cn.lcc.utils.endpoint;

import cn.hutool.crypto.SecureUtil;
import cn.lcc.utils.watch.*;
import com.alibaba.fastjson2.JSON;
import hy.iot.core.annotation.Endpoint;
import hy.iot.core.annotation.Mapping;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;


@Slf4j
@Endpoint
@Component
public class WatchEndpoint {

    @Mapping(pointType="UD", desc = "定位上传")
    public void UD_DEMOs(UD d, ChannelHandlerContext ctx) {
        System.out.println("定位上传"+ JSON.toJSONString(d)+" ==== "+ctx.name());
    }

    @Mapping(pointType="AL", desc = "SOS报警定位上传")
    public void AL(AL d, ChannelHandlerContext ctx) {
        log.info("sos 报警状态 {} {}",d.getStatus(),JSON.toJSONString(d));
        String str = getMd5Result(getWatchCommandLine(d.messageMark , d.deviceId , "AL"));
        ByteBuf response = ctx.alloc().buffer();
        response.writeBytes(str.getBytes());
        ctx.channel().writeAndFlush(response);
        log.info(" sos 报警回复 {} ",str);

        String find = getMd5Result(getWatchCommandLine(d.messageMark , d.deviceId , "FIND"));
        ByteBuf findres = ctx.alloc().buffer();
        findres.writeBytes(find.getBytes());
        ctx.channel().writeAndFlush(findres);
        log.info("查找手表位置 {} ",str);
    }


    @Mapping(pointType="Time", desc = "时间校对")
    public void watch_Time(Time d, ChannelHandlerContext ctx) {
        String str = getMd5Result(getWatchCommandLine(d.messageMark , d.deviceId , d.pointType,String.valueOf(System.currentTimeMillis()/1000)));
        log.info("ping 链路-时间校对 回复 {} ",str);
        ByteBuf response = ctx.alloc().buffer();
        response.writeBytes(str.getBytes());
        ctx.channel().writeAndFlush(response);
        log.info(" {} ",JSON.toJSONString(d));

//        String upload = getMd5Result(getWatchCommandLine(d.messageMark , d.deviceId , "UPLOAD","60"));
//        ByteBuf uploadresponse = ctx.alloc().buffer();
//        uploadresponse.writeBytes(upload.getBytes());
//        ctx.channel().writeAndFlush(uploadresponse);
//        log.info("定时上传频率 {} ",upload);


    }


    @Mapping(pointType="KA", desc = "链路保持-电量")
    public void watch_KA(KA d, ChannelHandlerContext ctx) {
        String str = getMd5Result(getWatchCommandLine(d.messageMark , d.deviceId , d.pointType));
        log.info("ping 链路 回复 {} ",str);
        ByteBuf response = ctx.alloc().buffer();
        response.writeBytes(str.getBytes());
        ctx.channel().writeAndFlush(response);
        log.info(" {} ",JSON.toJSONString(d));
    }


    @Mapping(pointType="PING", desc = "链路保持")
    public void watch_PING(PING d, ChannelHandlerContext ctx) {
        String str = getMd5Result(getWatchCommandLine(d.messageMark , d.deviceId , d.pointType,"1"));
        log.info("ping 链路 回复 {} ",str);
//        String msg = EscapeUtils.escape("你已离开岗位,请立即回到自己的工作区域");
//        String sendMsg = getMd5Result(getWatchCommandLine("" , d.deviceId , "MESSAGE",msg));
//        log.info("send msg {] {}",msg,sendMsg);
        ByteBuf response = ctx.alloc().buffer();
        response.writeBytes(str.getBytes());
        ctx.channel().writeAndFlush(response);

    }

    @Mapping(pointType="LK", desc = "心跳")
    public void LK(LK d, ChannelHandlerContext ctx) {
        String str = getMd5Result(getWatchCommandLine(d.messageMark , d.deviceId , d.pointType));
        log.info("心跳回复 {} ",str);
        ByteBuf response = ctx.alloc().buffer();
        response.writeBytes(str.getBytes());
        ctx.channel().writeAndFlush(response);
        System.out.println("===============LK"+ JSON.toJSONString(d)+" ==== "+ctx.name());
    }
    /**
     * appId：OS4yODM1NzU3MA==
     * appKey：259A401E6FA695FD668214AD8C66F43E
     * @param str
     */
    public String getMd5Result(String str){
        String appkey = "259A401E6FA695FD668214AD8C66F43E";
        String smd = SecureUtil.md5(str+appkey);
        return '['+smd+str+']';
    }

    public static final String watch_command = "%s*%s*%04x*%s";
    public static final String watch_command_params = "%s*%s*%04x*%s,%s";

    public static String getWatchCommandLine(String uuid,String deviceId, String command) {
        return getWatchCommandLine(uuid,deviceId, command, null);
    }

    public static String getWatchCommandLine(String uuid,String deviceId, String command, String params) {
        return StringUtils.isBlank(params) ? String.format(watch_command,uuid, deviceId, command.length(), command) : String.format(watch_command_params,uuid, deviceId, command.length() + params.length() + 1, command, params);
    }


}